class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;

        while (curr != null) {
            ListNode nextTemp = curr.next; // 保存当前节点的下一个节点
            curr.next = prev; // 反转当前节点的指针
            prev = curr; // prev 向前移动
            curr = nextTemp; // curr 向前移动
        }

        return prev; // prev 将是新链表的头
    }
}
